﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace MBS.UserControls.MovieScheduleUserControl
{
    public partial class AddMovieScheduleUserControl1 : System.Web.UI.UserControl
    {
        Dictionary<int, string> cinemaDictionary;
        Dictionary<int, int> screenDictionary;
        Dictionary<int, string> movieDictionary;
        MBSBLC.BLC blc;

        private bool actionresult;


        public delegate void MovieScheduleAddHandler();

        public event MovieScheduleAddHandler MovieScheduleAddComplete;
        public event MovieScheduleAddHandler MovieScheduleAddFailed;


        public bool Actionresult
        {
            get { return actionresult; }
            set { actionresult = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                blc = new MBSBLC.BLC();

                cinemaDictionary = blc.GetAllCinemas();
                cinemaDropDownList.DataSource = cinemaDictionary;
                cinemaDropDownList.DataTextField = "Value";
                cinemaDropDownList.DataValueField = "Key";
                cinemaDropDownList.DataBind();

                movieDictionary = blc.GetAllMovies();
                movieDropDownList.DataSource = movieDictionary;
                movieDropDownList.DataTextField = "Value";
                movieDropDownList.DataValueField = "Key";
                movieDropDownList.DataBind();
                if (cinemaDropDownList.SelectedValue.Equals(""))
                {
                    screenDictionary = new Dictionary<int, int>();
                }
                else
                {
                    screenDictionary = blc.GetAllScreensForACinema(Convert.ToInt32(cinemaDropDownList.SelectedValue));
                }
                screenDropDownList.DataSource = screenDictionary;
                screenDropDownList.DataTextField = "Value";
                screenDropDownList.DataValueField = "Key";
                screenDropDownList.DataBind();
            }
        }

        protected void movieDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void cinemaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            blc = new MBSBLC.BLC();
            if (cinemaDropDownList.SelectedValue.Equals(""))
            {
                screenDictionary = new Dictionary<int, int>();
            }
            else
            {
                screenDictionary = blc.GetAllScreensForACinema(Convert.ToInt32(cinemaDropDownList.SelectedValue));
            }
             screenDropDownList.DataSource = screenDictionary;
             screenDropDownList.DataTextField = "Value";
             screenDropDownList.DataValueField = "Key";
             screenDropDownList.DataBind();
        }

        protected void screenDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void AddSchedule_Click(object sender, EventArgs e)
        {
            DateTime StartDate = DateTime.Parse(StartDateTextBox.Text, new CultureInfo("en-US"));
            DateTime EndDate = DateTime.Parse(EndDateTextBox.Text, new CultureInfo("en-US"));
            string startTime = StartTimeTextBox.Text.Trim();
            int startHour = Convert.ToInt32(startTime.Substring(0, startTime.IndexOf(':')));
            int startMinute = Convert.ToInt32(startTime.Substring(startTime.IndexOf(':')+1));
            string endTime = EndTimeTextBox.Text.Trim();
            int endHour = Convert.ToInt32(endTime.Substring(0, endTime.IndexOf(':')));
            int endMinute = Convert.ToInt32(endTime.Substring(endTime.IndexOf(':')+1));
            
            TimeSpan ShowStartTime = new TimeSpan(startHour, startMinute, 0);
            TimeSpan ShowEndTime = new TimeSpan(endHour, endMinute, 0);
            
           // DateTime ShowStartTime = new DateTime(DateTime.MinValue.Year, DateTime.MinValue.Month, DateTime.MinValue.Day, startHour, startMinute, 0);
           // DateTime ShowEndTime = new DateTime(DateTime.MinValue.Year, DateTime.MinValue.Month, DateTime.MinValue.Day, endHour, endMinute, 0);
            double WeekDateFare = Convert.ToDouble(WeekdayFeeTextBox.Text);
            double WeekEndFare = Convert.ToDouble(WeekendFeeTextBox.Text);
            bool IsDeleted = false;
            int CinemaID = Convert.ToInt32(cinemaDropDownList.SelectedValue);
            int ScreenNo = Convert.ToInt32(screenDropDownList.SelectedValue);
            int MovieID = Convert.ToInt32(movieDropDownList.SelectedValue);
            blc = new MBSBLC.BLC();

            if (DateTime.Parse(StartDateTextBox.Text, new CultureInfo("en-US")).CompareTo(DateTime.Parse(EndDateTextBox.Text, new CultureInfo("en-US"))) > 0 || ShowStartTime.CompareTo(ShowEndTime) >= 0)
            {
            }
            else
            {
                actionresult = blc.AddMovieSchedule(
            StartDate,
            EndDate,
            ShowStartTime,
            ShowEndTime,
            WeekDateFare,
            WeekEndFare,
            IsDeleted,
           CinemaID,
            ScreenNo,
            MovieID);


                if (actionresult)
                {
                    MovieScheduleAddComplete();
                }
                else
                {
                    MovieScheduleAddFailed();
                }
            }
        }


        protected void ValidateDate(object source, ServerValidateEventArgs args)
        {

            if (DateTime.Parse(StartDateTextBox.Text, new CultureInfo("en-US")).CompareTo(DateTime.Parse(EndDateTextBox.Text, new CultureInfo("en-US"))) > 0)
            {
                args.IsValid = false;
            }
            else
            {

                args.IsValid = true;
            }
            
        }

        protected void ValidateTime(object source, ServerValidateEventArgs args)
        {

            string startTime = StartTimeTextBox.Text.Trim();
            int startHour = Convert.ToInt32(startTime.Substring(0, startTime.IndexOf(':')));
            int startMinute = Convert.ToInt32(startTime.Substring(startTime.IndexOf(':') + 1));
            string endTime = EndTimeTextBox.Text.Trim();
            int endHour = Convert.ToInt32(endTime.Substring(0, endTime.IndexOf(':')));
            int endMinute = Convert.ToInt32(endTime.Substring(endTime.IndexOf(':') + 1));

            TimeSpan ShowStartTime = new TimeSpan(startHour, startMinute, 0);
            TimeSpan ShowEndTime = new TimeSpan(endHour, endMinute, 0);
            if (ShowStartTime.CompareTo(ShowEndTime)>=0)
            {
                args.IsValid = false;
            }
            else
            {

                args.IsValid = true;
            }
        }

        /*
        protected void WeekdayFeeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Convert.ToDouble(WeekdayFeeTextBox.Text);
            } catch(Exception ex){
                WeekdayFeeTextBox.Text = "0.00";
            }
        }

        protected void WeekendFeeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Convert.ToDouble(WeekendFeeTextBox.Text);
            }
            catch (Exception ex)
            {
                WeekendFeeTextBox.Text = "0.00";
            }
        }
         */
    }
}